package com.rpframework.module.common.plugin.jpush;

import java.util.Date;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import cn.jpush.api.JPushClient;
import cn.jpush.api.common.APIConnectionException;
import cn.jpush.api.common.APIRequestException;
import cn.jpush.api.push.PushResult;
import cn.jpush.api.push.model.Message;
import cn.jpush.api.push.model.Platform;
import cn.jpush.api.push.model.PushPayload;
import cn.jpush.api.push.model.audience.Audience;
import cn.jpush.api.push.model.notification.Notification;

public class JPushApi {
	final static Logger LOG = LoggerFactory.getLogger(JPushApi.class);
	JPushClient jPushClient = null;
	public JPushClient getJPushClient() {
		if(jPushClient == null) {
			jPushClient = new JPushClient(JPushConfig.masterSecret, JPushConfig.appKey, JPushConfig.maxRetryTimes);
		}
		
		return jPushClient;
	}
	
	public void push(PushPayload payload) {
		try {
			PushResult result = getJPushClient().sendPush(payload);
			LOG.info("Got result - " + result);

		} catch (APIConnectionException e) {
			LOG.error("Connection error. Should retry later. ", e);

		} catch (APIRequestException e) {
			LOG.error("JPush Error response from JPush server. Should review and fix it. ",
					e);
			LOG.error("HTTP Status: " + e.getStatus());
			LOG.error("Error Code: " + e.getErrorCode());
			LOG.error("Error Message: " + e.getErrorMessage());
			LOG.error("Msg ID: " + e.getMsgId());
		}
	}

	public static PushPayload buildPushObjectAndroid() {
		return PushPayload
				.newBuilder()
				.setPlatform(Platform.android())
				.setAudience(Audience.all())
				.setNotification(
						Notification.android("only android " + new Date(),
								" hello android", null))

				.setMessage(Message.content("this hahahahahhaha"))

				.build();
	}

	public static PushPayload buildPushObjectIOS() {
		return PushPayload
				.newBuilder()
				.setPlatform(Platform.ios())
				.setAudience(Audience.all())
				// .setAudience(Audience.alias("123"))

				.setNotification(Notification.ios_set_badge(10))
				.setNotification(
						Notification.ios("Hello All IOS" + new Date(), null))
				.setMessage(Message.content("what are yous"))

				.build();
	}

//	public static PushPayload buildPushObject_all_all_alert() {
//		return PushPayload.alertAll(ALERT);
//	}
//
//	public static PushPayload buildPushObject_all_alias_alert() {
//		return PushPayload.newBuilder().setPlatform(Platform.all())
//				.setAudience(Audience.alias("alias1"))
//				.setNotification(Notification.alert(ALERT)).build();
//	}
//
//	public static PushPayload buildPushObject_android_tag_alertWithTitle() {
//		return PushPayload.newBuilder().setPlatform(Platform.android())
//				.setAudience(Audience.tag("tag1"))
//				.setNotification(Notification.android(ALERT, TITLE, null))
//				.build();
//	}
//
//	public static PushPayload buildPushObject_android_and_ios() {
//		return PushPayload
//				.newBuilder()
//				.setPlatform(Platform.android_ios())
//				.setAudience(Audience.all())
//				.setNotification(
//						Notification
//								.newBuilder()
//								.setAlert("这是推送内容,当前时间：" + new Date())
//								.addPlatformNotification(
//										AndroidNotification.newBuilder()
//												.setTitle("hah Title").build())
//								.addPlatformNotification(
//										IosNotification
//												.newBuilder()
//												.incrBadge(5)
//												.addExtra("key", "123")
//												.addExtra("context",
//														"extra content")
//												.build()).build()).build();
//	}
//
//	public static PushPayload buildPushObject_ios_tagAnd_alertWithExtrasAndMessage() {
//		return PushPayload
//				.newBuilder()
//				.setPlatform(Platform.ios())
//				.setAudience(Audience.all())
//				.setNotification(
//						Notification
//								.newBuilder()
//								.addPlatformNotification(
//										IosNotification.newBuilder()
//												.setAlert(ALERT).setBadge(5)
//												.setSound("happy")
//												.addExtra("from", "JPush")
//												.build()).build())
//				.setMessage(Message.content(MSG_CONTENT))
//				.setOptions(
//						Options.newBuilder().setApnsProduction(true).build())
//				.build();
//	}
//
//	public static PushPayload buildPushObject_ios_audienceMore_messageWithExtras() {
//		return PushPayload
//				.newBuilder()
//				.setPlatform(Platform.android_ios())
//				.setAudience(
//						Audience.newBuilder()
//								.addAudienceTarget(
//										AudienceTarget.tag("tag1", "tag2"))
//								.addAudienceTarget(
//										AudienceTarget
//												.alias("alias1", "alias2"))
//								.build())
//				.setMessage(
//						Message.newBuilder().setMsgContent(MSG_CONTENT)
//								.addExtra("from", "JPush").build()).build();
//	}
}
